
package com.sskj.dingtalk.service.impl;

import java.util.Calendar;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.SmartworkBlackboardListtoptenRequest;
import com.dingtalk.api.response.SmartworkBlackboardListtoptenResponse;
import com.dingtalk.api.response.SmartworkBlackboardListtoptenResponse.OapiBlackboardVo;
import com.sskj.core.exception.MException;
import com.sskj.dingtalk.exception.OApiException;
import com.sskj.dingtalk.helper.AuthHelper;
import com.taobao.api.ApiException;

@RestController
@RequestMapping(value = "/dingtalk", method = { RequestMethod.POST, RequestMethod.GET })
public class DingTalkNMessageServiceImpl {

	protected final Log logger = LogFactory.getLog(DingTalkMessageServiceImpl.class);

	@Autowired
	AuthHelper authHelper;

	// 获取通知公告
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/nmessage/list", method = { RequestMethod.GET })
	public SmartworkBlackboardListtoptenResponse getNMessageList(String userId) throws MException {

		try {

			DingTalkClient client = new DefaultDingTalkClient("https://eco.taobao.com/router/rest");

			SmartworkBlackboardListtoptenRequest req = new SmartworkBlackboardListtoptenRequest();

//			req.setUserid("036459134421334608");
			req.setUserid(userId);
			return client.execute(req, authHelper.getAccessToken());

		} catch (OApiException | ApiException e) {

			e.printStackTrace();
			return null;
		}
	}

	// 获取通知公告条数
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/nmessage/count", method = { RequestMethod.GET })
	public Integer geMessageCount(String userId) throws MException {
		Integer count = 0;
		try {

			DingTalkClient client = new DefaultDingTalkClient("https://eco.taobao.com/router/rest");

			SmartworkBlackboardListtoptenRequest req = new SmartworkBlackboardListtoptenRequest();

			//req.setUserid("036459134421334608");
			req.setUserid(userId);
			SmartworkBlackboardListtoptenResponse result = client.execute(req, authHelper.getAccessToken());

			if (result.getResult() != null && result.getResult().getSuccess()) {
				if(null != result.getResult().getBlackboardList())
				{
					for (OapiBlackboardVo vo : result.getResult().getBlackboardList()) {
	
						Long num = (Calendar.getInstance().getTimeInMillis() - vo.getGmtCreate().getTime()) / 1000 / 60 / 60
								/ 24;
	
						if (num.intValue() < 1) {
							count++;
						}
					}
				}
			}

			return count;

		} catch (OApiException | ApiException e) {

			e.printStackTrace();
			return count;
		}
	}

}
